﻿using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using Lamex.Common;
using Lamex.Controls;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Lamex.Library.CommonClass
{
    public class SetControlLanguage
    {
        public static void SetControlName(PanelControl panel,DataTable dt)
        {
            if(Loginer.CurrentUser.ChangeLanguage==0)
            {
                foreach (Control c in panel.Controls)
                {
                    if (c is ucTextEdit)
                    {
                        ucTextEdit uc = c as ucTextEdit;
                        DataRow dr = dt.Select(" 1=1 and WRITE_FIELD_NAME='" + uc.Name + "'").FirstOrDefault();
                        if (dr != null)
                            uc.Caption = dr["CTRL_CN_NAME"].ToString();
                        else
                        {
                            DataRow _dr = dt.Select(" 1=1 and REF_FIELD_NAME='" + uc.Name + "'").FirstOrDefault();
                            if (_dr != null)
                                uc.Caption = dr["CTRL_CN_NAME"].ToString();
                        }
                    }
                    else if (c is ucComboBoxEdit)
                    {
                        ucComboBoxEdit uc = c as ucComboBoxEdit;
                        DataRow dr = dt.Select(" 1=1 and WRITE_FIELD_NAME='" + uc.Name + "'").FirstOrDefault();
                        if (dr != null)
                            uc.Caption = dr["CTRL_CN_NAME"].ToString();
                        else
                        {
                            DataRow _dr = dt.Select(" 1=1 and REF_FIELD_NAME='" + uc.Name + "'").FirstOrDefault();
                            if (_dr != null)
                                uc.Caption = dr["CTRL_CN_NAME"].ToString();
                        }
                    }
                    else if (c is ucLookUpEdit)
                    {
                        ucLookUpEdit uc = c as ucLookUpEdit;
                        DataRow dr = dt.Select(" 1=1 and WRITE_FIELD_NAME='" + uc.Name + "'").FirstOrDefault();
                        if (dr != null)
                            uc.Caption = dr["CTRL_CN_NAME"].ToString();
                        else
                        {
                            DataRow _dr = dt.Select(" 1=1 and REF_FIELD_NAME='" + uc.Name + "'").FirstOrDefault();
                            if (_dr != null)
                                uc.Caption = dr["CTRL_CN_NAME"].ToString();
                        }
                    }
                }
            }
            else
            {
                foreach (Control c in panel.Controls)
                {
                    if (c is ucTextEdit)
                    {
                        ucTextEdit uc = c as ucTextEdit;
                        DataRow dr = dt.Select(" 1=1 and WRITE_FIELD_NAME='" + uc.Name + "'").FirstOrDefault();
                        if (dr != null)
                            uc.Caption = dr["CTRL_EN_NAME"].ToString();
                        else
                        {
                            DataRow _dr = dt.Select(" 1=1 and REF_FIELD_NAME='" + uc.Name + "'").FirstOrDefault();
                            if (_dr != null)
                                uc.Caption = dr["CTRL_EN_NAME"].ToString();
                        }
                    }
                    else if (c is ucComboBoxEdit)
                    {
                        ucComboBoxEdit uc = c as ucComboBoxEdit;
                        DataRow dr = dt.Select(" 1=1 and WRITE_FIELD_NAME='" + uc.Name + "'").FirstOrDefault();
                        if (dr != null)
                            uc.Caption = dr["CTRL_EN_NAME"].ToString();
                        else
                        {
                            DataRow _dr = dt.Select(" 1=1 and REF_FIELD_NAME='" + uc.Name + "'").FirstOrDefault();
                            if (_dr != null)
                                uc.Caption = dr["CTRL_EN_NAME"].ToString();
                        }
                    }
                    else if (c is ucLookUpEdit)
                    {
                        ucLookUpEdit uc = c as ucLookUpEdit;
                        DataRow dr = dt.Select(" 1=1 and WRITE_FIELD_NAME='" + uc.Name + "'").FirstOrDefault();
                        if (dr != null)
                            uc.Caption = dr["CTRL_EN_NAME"].ToString();
                        else
                        {
                            DataRow _dr = dt.Select(" 1=1 and REF_FIELD_NAME='" + uc.Name + "'").FirstOrDefault();
                            if (_dr != null)
                                uc.Caption = dr["CTRL_EN_NAME"].ToString();
                        }
                    }
                }
            }
            
        }

        public static void SetGcControlName(GroupControl gc, DataTable dt)
        {
          
            if (Loginer.CurrentUser.ChangeLanguage == 0)
            {
                foreach (Control c in gc.Controls)
                {
                    if (c is ucTextEdit)
                    {
                        ucTextEdit uc = c as ucTextEdit;
                        DataRow dr = dt.Select(" 1=1 and WRITE_FIELD_NAME='" + uc.Name + "'").FirstOrDefault();
                        if (dr != null)
                            uc.Caption = dr["CTRL_CN_NAME"].ToString();
                        else
                        {
                            DataRow _dr = dt.Select(" 1=1 and REF_FIELD_NAME='" + uc.Name + "'").FirstOrDefault();
                            if (_dr != null)
                                uc.Caption = dr["CTRL_CN_NAME"].ToString();
                        }
                    }
                    else if (c is ucComboBoxEdit)
                    {
                        ucComboBoxEdit uc = c as ucComboBoxEdit;
                        DataRow dr = dt.Select(" 1=1 and WRITE_FIELD_NAME='" + uc.Name + "'").FirstOrDefault();
                        if (dr != null)
                            uc.Caption = dr["CTRL_CN_NAME"].ToString();
                        else
                        {
                            DataRow _dr = dt.Select(" 1=1 and REF_FIELD_NAME='" + uc.Name + "'").FirstOrDefault();
                            if (_dr != null)
                                uc.Caption = dr["CTRL_CN_NAME"].ToString();
                        }
                    }
                    else if (c is ucLookUpEdit)
                    {
                        ucLookUpEdit uc = c as ucLookUpEdit;
                        DataRow dr = dt.Select(" 1=1 and WRITE_FIELD_NAME='" + uc.Name + "'").FirstOrDefault();
                        if (dr != null)
                            uc.Caption = dr["CTRL_CN_NAME"].ToString();
                        else
                        {
                            DataRow _dr = dt.Select(" 1=1 and REF_FIELD_NAME='" + uc.Name + "'").FirstOrDefault();
                            if (_dr != null)
                                uc.Caption = dr["CTRL_CN_NAME"].ToString();
                        }
                    }
                }
            }
            else
            {
                foreach (Control c in gc.Controls)
                {
                    if (c is ucTextEdit)
                    {
                        ucTextEdit uc = c as ucTextEdit;
                        DataRow dr = dt.Select(" 1=1 and WRITE_FIELD_NAME='" + uc.Name + "'").FirstOrDefault();
                        if (dr != null)
                            uc.Caption = dr["CTRL_EN_NAME"].ToString();
                        else
                        {
                            DataRow _dr = dt.Select(" 1=1 and REF_FIELD_NAME='" + uc.Name + "'").FirstOrDefault();
                            if (_dr != null)
                                uc.Caption = dr["CTRL_EN_NAME"].ToString();
                        }
                    }
                    else if (c is ucComboBoxEdit)
                    {
                        ucComboBoxEdit uc = c as ucComboBoxEdit;
                        DataRow dr = dt.Select(" 1=1 and WRITE_FIELD_NAME='" + uc.Name + "'").FirstOrDefault();
                        if (dr != null)
                            uc.Caption = dr["CTRL_EN_NAME"].ToString();
                        else
                        {
                            DataRow _dr = dt.Select(" 1=1 and REF_FIELD_NAME='" + uc.Name + "'").FirstOrDefault();
                            if (_dr != null)
                                uc.Caption = dr["CTRL_EN_NAME"].ToString();
                        }
                    }
                    else if (c is ucLookUpEdit)
                    {
                        ucLookUpEdit uc = c as ucLookUpEdit;
                        DataRow dr = dt.Select(" 1=1 and WRITE_FIELD_NAME='" + uc.Name + "'").FirstOrDefault();
                        if (dr != null)
                            uc.Caption = dr["CTRL_EN_NAME"].ToString();
                        else
                        {
                            DataRow _dr = dt.Select(" 1=1 and REF_FIELD_NAME='" + uc.Name + "'").FirstOrDefault();
                            if (_dr != null)
                                uc.Caption = dr["CTRL_EN_NAME"].ToString();
                        }
                    }
                }
            }

        }

        public static void SetGridViewControlName(GridView view, DataTable dt)
        {
            if (Loginer.CurrentUser.ChangeLanguage == 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    if (!string.IsNullOrEmpty(dr["WRITE_FIELD_NAME"].ToString()))
                    {
                        GridColumn col = ColumeName(dr["WRITE_FIELD_NAME"].ToString(), view);
                        if (col != null) col.Caption = dr["CTRL_CN_NAME"].ToString();
                    }
                    else
                    {
                        GridColumn col = ColumeName(dr["REF_FIELD_NAME"].ToString(), view);
                        if (col != null) col.Caption = dr["CTRL_CN_NAME"].ToString();
                    }
                }
            }
           else
            {
                foreach (DataRow dr in dt.Rows)
                {
                    if (!string.IsNullOrEmpty(dr["WRITE_FIELD_NAME"].ToString()))
                    {
                        GridColumn col = ColumeName(dr["WRITE_FIELD_NAME"].ToString(),view);
                        if (col != null) col.Caption = dr["CTRL_EN_NAME"].ToString();
                    }
                    else
                    {
                        GridColumn col = ColumeName(dr["REF_FIELD_NAME"].ToString(),view);
                        if (col != null) col.Caption = dr["CTRL_EN_NAME"].ToString();
                    }

                }
            }
        }
        private static GridColumn ColumeName(string colName, GridView view)
        {
            GridColumn gridColumn = null;
            foreach (GridColumn c in view.Columns)
            {
                if (c.FieldName == colName) return c;
            }
            return gridColumn;
        }
    }
}
